package com.acwing.partition38;

import java.io.*;

/**
 * @author rkc
 * @date 2022/8/9 9:23
 */
public class AC3712根能抵达的点 {

    private static final int N = 20010, M = 2 * N;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    private static int n, y, idx;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int t = nextInt();
        while (t-- > 0) {
            n = nextInt(); y = nextInt();
            idx = 0;
            for (int i = 0; i < N; i++) h[i] = -1;
            for (int i = 0; i < n - 1; i++) {
                int a = nextInt(), b = nextInt(), c = nextInt();
                add(a, b, c);
                add(b, a, c);
            }
            writer.println(search());
        }
        writer.flush();
    }

    private static int search() {
        int l = 0, r = (int) 1e7;
        while (l < r) {
            int mid = l + r >> 1;
            if (dfs(0, -1, mid) > y) l = mid + 1;
            else r = mid;
        }
        return l;
    }

    private static int dfs(int u, int fa, int limit) {
        int res = 1;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (w[i] >= limit && v != fa) {
                res += dfs(v, u, limit);
            }
        }
        return res;
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
